#include "stdlib.h"
#include "string.h"
#include <hidef.h> /* for EnableInterrupts macro */
#include "Headers/derivative.h" /* include peripheral declarations */
#include "Headers/define.h"
#include "Headers/232.h"
#include "Headers/dvr_i2cVirtual.h"
#include "Headers/GPIO.h"
#include "Headers/ICG.h"
#include "Headers/interrupt.h"
#include "Headers/config_micro.h"
#include "Headers/time.h"
void main(void) {
  char *tx_232, error_fn, memory_addr, num_bytes;
  SetClock_FEI(); /* set bus clock to 16 MHz */
  rx_232 = malloc(30);
  tx_232 = malloc(30);
  EnableInterrupts; /* enable interrupts */
  /* include your code here */
  IRQSC = IRQSC_IRQPE_MASK | IRQSC_IRQACK_MASK | IRQSC_IRQIE_MASK;/* IRQ pin */
  
  ledandpush_configuration();
  modem_config();
  setup_232(Baud9600, Normal_wire);
  
  
  
  //error_fn = ifnWrite_byte(ADDR_SLAVE, 0, 25, "HUGO GERMAN SERVIN GARCIA");
  //error_fn = ifnSelective_read(ADDR_SLAVE, 0, 25, rx_232);

  
  for(;;) {
    __RESET_WATCHDOG(); /* feeds the dog */
    if(rx_flag){
      rx_flag = 0;
      /*
      * comandos desde terminal para poder excribir y leer en la
      * memoria IIC
      * r|addres memory|number bytes      1|0|10
      * w|addres memory|number bytes|data 0|0|10|hugo germa
      */
      rx_232 = strtok(rx_232, "|");
      if(*rx_232 == 1){
        rx_232 = strtok(NULL, "|");
        memory_addr = *rx_232;
        rx_232 = strtok(NULL, "|");
        num_bytes = *rx_232;
        error_fn = ifnSelective_read(ADDR_SLAVE, memory_addr, num_bytes, tx_232);
      } else if(*rx_232 == 0){
        rx_232 = strtok(NULL, "|");
        memory_addr = *rx_232;
        rx_232 = strtok(NULL, "|");
        num_bytes = *rx_232;
        rx_232 = strtok(NULL, "|");
        error_fn = ifnWrite_byte(ADDR_SLAVE, memory_addr, num_bytes, rx_232);
      } else{
        error_fn = send_232("comando desconocido");
        delay_ms(3000);
        error_fn = send_232("                   \r");
      }
      ld3 ^= 1;
      
    }
  } /* loop forever */
  /* please make sure that you never leave main */
}
